package org.msfj.toilets.socket;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Random;

/**
 * @Description:
 * @Author: like
 * @Date: 2018-04-10 17:21
 * @Version: 1.0
 * @Created in idea by autoCode
 */
public class UdpClient {
    private static byte[] temperature = {
            (byte) 0xfe, (byte) 0x5c, (byte) 0x4b, (byte) 0x89,
            (byte) 0x27, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x65,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x14, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x03, (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x05, (byte) 0x32, (byte) 0x31, (byte) 0x2e, (byte) 0x31, (byte) 0x30,
            (byte) 0x04, (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x05, (byte) 0x32, (byte) 0x35, (byte) 0x2e, (byte) 0x33, (byte) 0x37,
            (byte) 0xff, (byte) 0xff};
    private static byte[] infrared = {
            (byte) 0xfe, (byte) 0x5c, (byte) 0x4b, (byte) 0x89,
            (byte) 0x20, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x65,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x0d, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x06, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0xff, (byte) 0xff};

    public static void main(String[] args) throws IOException {
        try {
            System.out.println("客户端启动成功");
            String netAddress = "127.0.0.1";
            final int PORT_NUM = 9091;
            DatagramSocket datagramSocket;
            DatagramPacket datagramPacket;
            datagramSocket = new DatagramSocket();
            InetAddress address = InetAddress.getByName(netAddress);
            while (true) {
                Random random = new Random();
                int i = random.nextInt(10);
                if (i >= 5) {
                    datagramPacket = new DatagramPacket(temperature, temperature.length, address, PORT_NUM);
                    // 发送数据
                    datagramSocket.send(datagramPacket);
                    System.out.println("发送数据1");
                } else {
                    datagramPacket = new DatagramPacket(infrared, infrared.length, address, PORT_NUM);
                    // 发送数据
                    datagramSocket.send(datagramPacket);
                    System.out.println("发送数据2");
                }
            }
        } catch (Exception e) {
            System.out.println("can not listen to:" + e);// 出错，打印出错信息
        }
    }

}
